package com.jiancong.service.impl;

import com.jiancong.domain.ResponseResult;
import com.jiancong.domain.entity.Menu;
import com.jiancong.domain.vo.MenuTreeVo;
import com.jiancong.domain.vo.RoleMenuTreeResponse;
import com.jiancong.mapper.MenuMapper;
import com.jiancong.mapper.RoleMenuMapper;
import com.jiancong.service.MenuService;
import com.jiancong.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 菜单权限表(Menu)表服务实现类
 *
 * @author makejava
 * @since 2024-05-08 19:07:14
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<String> selectPermsByUserId(Long id) {
        //如果是管理员，返回所有的权限
        if(SecurityUtils.isAdmin()){
            return menuMapper.selectAllPerms();

//            LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
//            wrapper.in(Menu::getMenuType,SystemConstants.MENU,SystemConstants.BUTTON);
//            wrapper.eq(Menu::getStatus,SystemConstants.STATUS_NORMAL);
//            List<Menu> menus = list(wrapper);
        }
        //返回所具有的权限

        return menuMapper.selectPermsByUserId(id);



    }

    @Override
    @Transactional
    public ResponseResult insertMenu(Menu menu) {
        Long userId = SecurityUtils.getUserId();
        menu.setCreateBy(userId);
        menu.setUpdateBy(userId);
        menu.setCreateTime(new Date());
        menu.setUpdateTime(new Date());
        menuMapper.insertMenu(menu);
        return ResponseResult.okResult();
    }
    @Override
    public List<MenuTreeVo> selectTreeselect() {
        List<MenuTreeVo> menus = menuMapper.selectAllMenu();
        return buildTree(menus, 0L); // 假设根菜单的parentId为0
    }

    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Override
    @Transactional
    public RoleMenuTreeResponse getRoleMenuTreeAndPermissions(Long id) {
        List<MenuTreeVo> menus = menuMapper.selectAllMenu();
        List<MenuTreeVo> menuTreeVos = buildTree(menus, 0L);
        List<Long> checkedKeys = roleMenuMapper.selectMenuIdsByRoleId(id);
        RoleMenuTreeResponse response = new RoleMenuTreeResponse();
        response.setMenus(menuTreeVos);
        response.setCheckedKeys(checkedKeys);
        return response;
    }

    private List<MenuTreeVo> buildTree(List<MenuTreeVo> menus, Long parentId) {
        List<MenuTreeVo> children = new ArrayList<>();
        for (MenuTreeVo menu : menus) {
            if (menu.getParentId().equals(parentId)) {
                List<MenuTreeVo> childMenus = buildTree(menus, menu.getId());
                menu.setChildren(childMenus);
                children.add(menu);
            }
        }
        return children;
    }

    @Override
    public Menu selectMenuById(Long id) {
        return menuMapper.selectMenuById(id);
    }

    public boolean updateMenu(Menu menu) {
        Long userId = SecurityUtils.getUserId();
        menu.setCreateBy(userId);
        menu.setUpdateBy(userId);
        menu.setCreateTime(new Date());
        menu.setUpdateTime(new Date());
        int updated = menuMapper.updateMenu(menu);
        return updated > 0;
    }

    public boolean isSelfParent(Menu menu) {
        Menu currentMenu = selectMenuById(menu.getId());
        if (currentMenu != null && currentMenu.getParentId().equals(menu.getParentId())) {
            return true;
        }
        return false;
    }



    @Override
    public List<Menu> selectRouterMenuTreeByUserId(Long userId) {
        List<Menu> menus = null;
        //判断是否是管理员
        if(SecurityUtils.isAdmin()){
        //如果是 获取所有符合要求的Menu
                    menus = menuMapper.selectAllRouterMenu();
        }else{
        //否则 获取当前用户所具有的Menu
            menus = menuMapper.selectRouterMenuTreeByUserId(userId);
        }
        //构建tree
        //先找出第一层的菜单 然后去找他们的子菜单设置到children属性中
        List<Menu> menuTree = builderMenuTree(menus,0L);
        return menuTree;
    }

    @Override
    public List<Menu> getMenuList(String menuName,String status) {
        return menuMapper.getMenuList(menuName,status);
    }




    private List<Menu> builderMenuTree(List<Menu> menus, Long parentId) {
        List<Menu> menuTree = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId().equals(parentId)) {
                menu.setChildren(getChildren(menu,menus));
                menuTree.add(menu);
            }
        }
        return menuTree;
    }

    private List<Menu> getChildren(Menu menu, List<Menu> menus) {
        List<Menu> childrenList = new ArrayList<>();
        for (Menu m : menus) {
            if (m.getParentId().equals(menu.getId())) {
                Menu setChildren = m.setChildren(getChildren(m, menus));
                childrenList.add(setChildren);
            }
        }
        return childrenList;
    }


}
